﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class my : MonoBehaviour {
    private Animator animator;
    // Use this for initialization
    private GameObject hero;
    public float MoveSpeed = 3.0F;
    public float AttackDistance = 4.0F;
    private AnimatorStateInfo animatorInfo;
    public int Health;
    private int attacking=1;
    private bool busy=false;
    private bool right = true;
    private float Flytime,Ctime;
    private string beattacked;
    private string LBeAttacked="";
    private float Lhard=-5;
    private float HardTime;
    private float LastY, NowY;
    private bool OnGround;
    public int get_attacking() {
        return attacking;
    }
    public void OnGroundWay(bool b) {

        OnGround = b;
    }
    public void set_beattacked(string str) {
        beattacked = str;
    }
   public  void set_attacking() {
        attacking = 0;
    }
    public void SetHardTime(float t) {
        HardTime = t;
        Lhard = Time.time;

    }
    void Start () {
       // transform.FindChild("Canvas").GetComponent<>().enabled = false;
       // HpBar = GetComponent<Canvas>().GetComponent<Slider>();
        //if(HpBar)
       // HpBar.value = 1.0f;
        animator = GetComponent<Animator>();
        Flytime =-5.0f;
        hero = GameObject.Find("Garrosh");
        gameObject.transform.FindChild("my_knife").gameObject.GetComponent<PolygonCollider2D>().enabled = false;
    }

    void Update () {
        //Update存储AI行为。AI在执行部分动作时候无法进行选择行为（正在攻击，被攻击，死亡），所以busy直接return
        //因此，busy只对AI行为上锁。

        transform.FindChild("Canvas").transform.FindChild("Slider").SendMessage("show", Health);
            if (animator.GetCurrentAnimatorStateInfo(0).IsName("attacked") || animator.GetCurrentAnimatorStateInfo(0).IsName("die"))
            busy = true;
        if (busy) return;
        if (hero.transform.position.x - transform.position.x > 0&&right) {
            transform.Rotate(new Vector3(0, 180, 0));
            transform.FindChild("Canvas").transform.Rotate(new Vector3(0, 180, 0));
            right = false;
            return;
        }
        if (hero.transform.position.x - transform.position.x < 0 && !right) {
            transform.Rotate(new Vector3(0, 180, 0));
            transform.FindChild("Canvas").transform.Rotate(new Vector3(0, 180, 0));
            right = true;
            return;

        }

        if (LastY != transform.position.y)
        {
            LastY = transform.position.y;
            return;
        }
        LastY = transform.position.y;

        if (Mathf.Abs(hero.transform.position.x - transform.position.x) < AttackDistance&& Mathf.Abs(hero.transform.position.y - transform.position.y) < AttackDistance)
            Attack();
        else
            Walk();
    }

    void LateUpdate()
    {
        //每一帧结束进行判断再进行释放资源等等。
     

        animatorInfo = animator.GetCurrentAnimatorStateInfo(0);
        if ((animatorInfo.normalizedTime > 1) && (animatorInfo.IsName("my_die"))){
            Destroy(this.gameObject);
        }
        if (Health <= 0)
        {
            Die();
            return;
        }
        if (animatorInfo.IsName("my_idle")||(animatorInfo.normalizedTime > 1) && (animatorInfo.IsName("my_rest"))) {
            attacking = 1;
            busy = false;
        }
        if ((Time.time - Flytime < 1)) {
            transform.Translate(Vector3.up * Time.deltaTime * MoveSpeed*10);
            animator.Play("my_fly");
        }

    }
    public void KnockFly(float t) {
        Flytime = Time.time;
    }
    public void TakeDamage(int damage)
    {
        if (beattacked.Equals(LBeAttacked))
        {
            return;
        }
        else {
            LBeAttacked = beattacked;
        }
        //由攻击者调用被攻击者的收到伤害行为，传递伤害参数。伤害存储在攻击者.攻击部件.碰撞器中。
        Health -= damage;
        Debug.Log("my:" + Health);
        if (Health <= 0)
        {
            Die();
        }
        else
        {
            busy = true;
            animator.Play("my_attacked");
        }
    }
    void Die()
    {
        busy = true;
        animator.Play("my_die");
    }
    void Attack()
    {
        //update判定行为调用
        //判断是否可以攻击，构成条件，执行攻击。
        //Debug.Log("attack");
        animatorInfo = animator.GetCurrentAnimatorStateInfo(0);
        busy = true;
        gameObject.transform.FindChild("my_knife").gameObject.GetComponent<PolygonCollider2D>().enabled =true;
        //仅在攻击时候开启攻击部位的触发器
        animator.Play("my_attack");
    }


    void Walk() {
        if(animatorInfo.IsName("my_rest")) gameObject.transform.FindChild("my_knife").gameObject.GetComponent<PolygonCollider2D>().enabled = false;
        animator.Play("my_walkd");
        transform.Translate(Vector3.left* Time.deltaTime * MoveSpeed);
    }

}
